package com.clientmanage.controller;

import com.clientmanage.domain.vo.clientInfoVo;
import com.clientmanage.service.ICustomerService;
import com.clientmanage.domain.resp.PageResult;
import com.clientmanage.domain.resp.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户管理控制器
 * 实现客户信息管理相关接口
 */
@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @GetMapping("/test")
    public String test(){
        return "test success";
    }
    /**
     * 获取客户列表
     * 接口地址：GET /customers/page-list
     */
    @GetMapping("/page-list")
    public Result<PageResult<clientInfoVo>> getCustomerList(
        @RequestParam(required = false) String name,
        @RequestParam(required = false) String phone,
        @RequestParam(required = false) String petName,
        @RequestParam(required = false) String petBreed,
        @RequestParam(defaultValue = "1") int pageNum,
        @RequestParam(defaultValue = "10") int pageSize
    ) {
        List<clientInfoVo> list = customerService.getClientInfoList(name, phone, petName, petBreed, pageNum, pageSize);
        return Result.success(new PageResult<>((long) pageSize, (long) pageNum, list, (long) list.size()));
    }

    /**
     * 新增客户
     * 接口地址：POST /customers
     */
    @PostMapping
    public Result<Void> addCustomer(@RequestBody clientInfoVo vo) {
        customerService.saveOrUpdateClientInfo(vo);
        return Result.success();
    }

    /**
     * 编辑客户
     * 接口地址：PUT /customers/{id}
     */
    @PutMapping("/{id}")
    public Result<Void> updateCustomer(@PathVariable String id, @RequestBody clientInfoVo vo) {
        vo.setId(id);
        customerService.saveOrUpdateClientInfo(vo);
        return Result.success();
    }

    /**
     * 删除客户
     * 接口地址：DELETE /customers/{id}
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteCustomer(@PathVariable String id) {
        customerService.deleteCustomerById(id);
        return Result.success();
    }

    /**
     * 新增宠物
     * 接口地址：POST /customers/{customerId}/pets
     */
    @PostMapping("/{customerId}/pets")
    public Result<Void> addPet(@PathVariable String customerId, @RequestBody clientInfoVo vo) {
        vo.setCustomerId(customerId);
        customerService.addPet(vo);
        return Result.success();
    }

    /**
     * 编辑宠物
     * 接口地址：PUT /pets/{id}
     */
    @PutMapping("/pets/{id}")
    public Result<Void> updatePet(@PathVariable String id, @RequestBody clientInfoVo vo) {
        vo.setPetId(String.valueOf(Long.valueOf(id)));
        customerService.updatePet(vo);
        return Result.success();
    }

    /**
     * 删除宠物
     * 接口地址：DELETE /pets/{id}
     */
    @DeleteMapping("/pets/{id}")
    public Result<Void> deletePet(@PathVariable String id) {
        customerService.deletePetById(id);
        return Result.success();
    }

    /**
     * 新增疫苗记录
     * 接口地址：POST /customer/vaccine/add
     */
    @PostMapping("/vaccine/add")
    public Result<Void> addVaccine(@RequestBody clientInfoVo vo) {
        customerService.addVaccine(vo);
        return Result.success();
    }

    /**
     * 更新疫苗记录
     * 接口地址：PUT /customer/vaccine/update
     */
    @PutMapping("/vaccine/update")
    public Result<Void> updateVaccine(@RequestBody clientInfoVo vo) {
        customerService.updateVaccine(vo);
        return Result.success();
    }

    /**
     * 删除疫苗记录
     * 接口地址：DELETE /customer/vaccine/delete/{id}
     */
    @DeleteMapping("/vaccine/delete/{id}")
    public Result<Void> deleteVaccine(@PathVariable String id) {
        customerService.deleteVaccineById(id);
        return Result.success();
    }

    /**
     * 升级为会员
     * 接口地址：POST /customers/upgrade
     */
    @PostMapping("/upgrade")
    public Result<Void> upgradeToMember(@RequestBody clientInfoVo vo) {
        customerService.upgradeToMember(vo.getCustomerId(), vo.getMemberLevel());
        return Result.success();
    }
}
